#Cleaned Version Of Code - Produces all Attention Analyses
rm(list=ls())
## setwd("")
library(xts)
library(stringr)
library(lubridate)
library(lmtest)
library(nlme)
library(ggplot2)
library(scales)
library(sandwich)
#Data loading
load("AttentionConsistent.RData")
combined$Date <- with(combined, as.Date(paste(year(Date), month(Date), "01", sep = "-")))
## combined[,"Date"]<-as.Date(combined[,"Date"])
combined<-xts(combined[,c("Product","IsFopo","Duration","indic","words")],order.by=combined[,"Date"])
finalAttention<-apply.monthly(combined[,c("Product","IsFopo","Duration","indic","words")],colSums,na.rm=T)
#Remove the zero durations - these are nonsense
finalAttention<-finalAttention[finalAttention[,"Duration"]>0,]
#A handful of garbage dates fall outside the window
finalAttention<-finalAttention["1933-01-01/1993-01-31"]



#Calculate proportion of months with data
months<-month(finalAttention)
years<-year(finalAttention)
newDates<-as.Date(paste0(years,"-",months,"-","01"))
newDates<-xts(rep(1,length(newDates)),order.by=newDates)
allMonths<-seq(from=as.Date("1933-03-01"),to=as.Date("1990-12-31"),by="month")
allMonths<-xts(rep(1,length(allMonths)),order.by=allMonths)
together<-cbind(newDates,allMonths)
sum(together[,1],na.rm=T)/nrow(together)



#Total Entries
nrow(combined)
#Unique Months of Data
nrow(finalAttention)

#Build augmented data frame for easy analysis
augmented<-cbind(finalAttention,month(finalAttention),year(finalAttention))
colnames(augmented)[6:7]<-c("Month","Year")
iselection<-augmented[,"Year"]%%4==0
midterm<-augmented[,"Year"]%%4==2
augmented<-cbind(augmented,iselection,midterm)
colnames(augmented)[8:9]<-c("iselection","ismidterm")
proximate<-augmented[,"Month"]%in%c(6,7,8,9,10,11)&augmented[,"iselection"]
augmented<-cbind(augmented,proximate)
colnames(augmented)[10]<-"proximate"
premidterm<-augmented[,"Month"]%in%c(6,7,8,9,10,11)&augmented[,"ismidterm"]
augmented<-cbind(augmented,premidterm)
colnames(augmented)[11]<-"premidterm"
inWindow<-augmented[,"Month"]%in%c(6,7,8,9,10,11)
augmented<-cbind(augmented,inWindow)
colnames(augmented)[12]<-"inWindow"
augmented<-cbind(augmented,NA)
#Fixed effects are assembled at the President-term level - i.e., switch to a new FE
#When President changes (incl. via death) or after a subsequent innauguration
colnames(augmented)[13]<-"Term"
augmented["1933-03-01/1937-01-31","Term"]<-1
augmented["1937-02-01/1941-01-31","Term"]<-2
augmented["1941-02-01/1945-03-31","Term"]<-3
augmented["1945-04-01/1949-01-31","Term"]<-4
augmented["1949-02-01/1953-01-31","Term"]<-5
augmented["1953-02-01/1957-01-31","Term"]<-6
augmented["1957-02-01/1961-01-31","Term"]<-7
augmented["1961-02-01/1963-11-31","Term"]<-8
augmented["1963-12-01/1965-01-31","Term"]<-9
augmented["1965-02-01/1969-01-31","Term"]<-10
augmented["1969-02-01/1973-01-31","Term"]<-11
augmented["1973-02-01/1974-08-01","Term"]<-12
augmented["1974-08-01/1977-01-31","Term"]<-13
augmented["1977-02-01/1981-01-31","Term"]<-14
augmented["1981-02-01/1985-01-31","Term"]<-15
augmented["1985-02-01/1989-01-31","Term"]<-16
augmented["1989-02-01/1993-01-31","Term"]<-17

#Figure 1 in Paper
#remove noisy diaries
finalAttention2<-finalAttention[finalAttention[,"Duration"]>1000,]


#Put in an NA so the plot doesn't join from Truman to Johnson
newentry<-xts(rbind(c(NA,NA,NA,NA,NA)),order.by=as.Date("1962-03-01"))
the.df <- rbind(finalAttention2,newentry)

## Figure 1
if (F) {
## png("FopoProportion2.png", width = 800, height = 400)
pdf("FopoProportion2.pdf", width = 10, height = 5)
g <- ggplot(the.df) +
  geom_line(aes(x=index(the.df), y=Product/Duration))
g <- g + annotate(
  "rect",
  xmin=as.Date(paste(seq(1936, 1988, 4), "-01-01", sep = "")),
  xmax=as.Date(paste(seq(1936, 1988, 4), "-12-31", sep = "")),
  ymin=-Inf, ymax=+Inf, fill=alpha('blue', 1/20)
)
g <- g + ylim(c(0, 0.5))
g <- g + xlab("Date") + ylab("Foreign Policy Duration / Total Recorded Activity")
g <- g + theme(panel.background = element_rect(fill = 'white', color = 'white'))
plot(g)
dev.off()
}

#Max Attention - Reported in Text of Paper
finalAttention[which.max(finalAttention[,"Product"]),]
#Roosevelt Attention - Reported in Text of Paper
mean(augmented["1933-01-01/1939-09-01","Product"])/60
mean(augmented["1939-09-01/1941-12-01","Product"])/60
mean(augmented["1941-12-01/1945-04-01","Product"])/60

mean(augmented["1945-09-01/1950-06-01"])/60
mean(augmented["1950-06-01/1953-01-31"])/60
#Table 3 in Paper
#Duration on election reported in paper text
t.test(Duration/60~iselection,data=augmented)
#Fopo on election reported in paper text
t.test(Product/60~iselection,data=augmented)
#Fopo on election only for proximate months
t.test(Product/60~iselection,data=augmented[inWindow,])

reelect<-augmented[,"proximate"]&(!augmented[,"Year"]%in%c(1952,1960,1968,1988))
norelect<-augmented[,"proximate"]&(augmented[,"Year"]%in%c(1952,1960,1968,1988))


#Model 1 of Table 3
model1<-lm(Product/60~proximate+as.character(Month),data=augmented)
coeftest(model1,vcov=vcovHAC(model1))
#Model 2
model2<-lm(Product/60~proximate+as.character(Month)+as.character(Term),data=augmented)
coeftest(model2,vcov=vcovHAC(model2))
#Model 3
model3<-lm(Product/60~proximate+as.character(Month)+as.character(ceiling(Year/2)),data=augmented)
coeftest(model3,vcov=vcovHAC(model3))
#Model 4
model4<-lm(Product/60~proximate+premidterm+as.character(Month),data=augmented)
coeftest(model4,vcov=vcovHAC(model4))
#Model 5
model5<-lm(Product/60~proximate+premidterm+as.character(Month)+as.character(Term),data=augmented)
coeftest(model5,vcov=vcovHAC(model5))
#Model 6
model6<-lm(Product/60~proximate+premidterm+as.character(Month)+as.character(ceiling(Year/2)),data=augmented)
coeftest(model6,vcov=vcovHAC(model6))
